//
//  BMLoginController.m
//  特种车调度
//
//  Created by 陈宇 on 15/8/19.
//
//

#import "BMLoginController.h"
#import "User.h"
#import "BMAccountManager.h"
#import "ZDCreateOrderController.h"


@interface BMLoginController ()

@property (weak, nonatomic) IBOutlet UITextField *uidTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *forgetButton;

- (IBAction)backAction:(UIBarButtonItem *)sender;
- (IBAction)loginAction:(UIButton *)sender;

@end

@implementation BMLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"您好";
    
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    // white.png图片自己下载个纯白色的色块，或者自己ps做一个
    [navigationBar setBackgroundImage:[UIImage imageWithColor:kGlobalColor]
                       forBarPosition:UIBarPositionAny
                           barMetrics:UIBarMetricsDefault];
    [navigationBar setShadowImage:[UIImage new]];
    
    NSAttributedString *attrTitle = [[NSAttributedString alloc] initWithString:@"忘记密码？" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont systemFontOfSize:12.f], NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}];
    [self.forgetButton setAttributedTitle:attrTitle forState:UIControlStateNormal];
}

- (IBAction)backAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)loginAction:(UIButton *)sender {
    NSString *username = _uidTextField.text;
    if ([BMUtils isEmptyString:username]) {
        [self showHint:@"UID/手机号不能为空!"];
        return;
    }
    NSString *password = _passwordTextField.text;
    if (password.length < 6 || password.length > 16) {
        [self showHint:@"请输入6-16位密码!"];
        return;
    }
    weakSelf(self);
    BMRequest *request = [BMRequest requestWithPath:@"login.htm" contentKey:kRoleType ? @"dispatcher" : @"owner"];
    request.params = @{@"username": username, @"password": password, @"role": kRole};
    [self showHudWithhint:kDefaultMessage];
    [User postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == 1) {
            [BMAccountManager sharedInstance].user = response.result;
            ZDCreateOrderController *createVC = (id)self.presentingViewController;
            [self dismissViewControllerAnimated:YES completion:^{
                if (weakSelf.type == 1) {
                    [createVC sendAction:nil];
                }
            }];
        } else {
            [self showHint:@"该账号或密码有误"];
        }
    }];
}

@end
